<template>
	<view class="gui-padding">
		<view class="gui-padding text-center font-bold m-100">
			{{msg}}
		</view>
		<view style="display: flex;gap: 20rpx;position: fixed;bottom: 20rpx;width: 90%;padding: 20rpx;" >
			<button @click="back" style="width: 30%;" >返回</button>
			<button type="primary" @click="scan1" style="width: 70%;">扫一扫</button>
			
		</view>
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" permissionID="CAMERA"></yk-authpup>

	</view>
</template>

<script>
import { nextTick } from "vue";
import ykAuthpup from "@/components/yk-authpup/yk-authpup";


	export default {
		data() {
			return {
				msg:"启动中..",
				user:null,
				qrcode:""
			}
		},
		  components: {
		        ykAuthpup
		    },
		onLoad() {
			let self=this;
			setTimeout(function(){
				self.scan1();
			},1000);
			
		},
		
		methods: {
			scan1(){
				this.$refs['authpup'].open()
			},
						
			 changeAuth(){
			            //这里是权限通过后执行自己的代码逻辑
			            console.log('权限已授权，此时可以调用uni的chooseImage等api了');
						let self=this;
						// uni.showModal({
						// 	title:"权限申请",
						// 	content:"是否允许调用相机的权限，用于扫码功能",
						// 	confirmText:"允许",
						// 	cancelText:"不允许",
						// 	success(res) {
						// 		if(res.confirm){
									self.scan();
						// 		}else{
						// 			self.msg="无法访问相机权限"
						// 		}
						// 	},
							
						// })
						
			        },
					
			back(){
				uni.navigateBack();
			},
			scan(){
				let self=this;
				uni.scanCode({
					success: function (res) {
						self.qrcode=res.result.qr_code;
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let d=JSON.parse(res.result)
						console.log(d);
						console.log(d.qr_code);
						let user=uni.getStorageSync("userInfo")
						console.log("user",user);
						self.msg="加载中...";
						if(res.scanType=="QR_CODE"){
							self.qrcode=d.qr_code;
							if(d.qr_code.indexOf("login")>=0){
								//扫码登录 
								self.$boya.NPost("/auth/qr/confirm",{
									qr_code:d.qr_code,
									user_id:user.id
								},function(res){
									console.log("confirm",res);
									if(res.code==200){										
										self.msg=res.data;
										uni.showToast({
											title:res.data+",3秒后返回",
											duration:3000,
											icon:"none"
										})
										setTimeout(function(){
											uni.navigateBack();
										},3000)
																				
									}else{
										self.msg=res.msg;
									}
								},function(err){
									console.log("扫码登录失败",err);
								})
							}
						}
						
					},
					fail:function(res){
						console.log("fail",res);
						self.msg="扫码取消或失败！";
						
					}
				});
			}
		}
	}
</script>

<style>

</style>
